package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.Course;

import com.offcn.service.CourseService;
import com.offcn.service.CourseServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.FileUploadUtil;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 课程类Servlet
 */
@WebServlet("/course")
@MultipartConfig  //处理文件上传数据
public class CourseServlet extends BaseServlet {
    //实例化service
    CourseService courseService = new CourseServiceImpl();
    //创建响应数据工具类
    ResultVO resultVO = null;
    //创建转换数据类型工具类对象
    ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 分页查询
     */
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取参数
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");
        //调用service层查询
        PageUtils<Course> pageUtils = courseService.findByPage(currentPage,pageSize,search);
        if(pageUtils != null){
            resultVO = new ResultVO(200,"查询成功",pageUtils);
        }else {
            resultVO = new ResultVO(500,"查询失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 批量删除
     */
    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String[] cids = req.getParameterValues("cid");
        String[] courseImages = req.getParameterValues("courseImage");
        String[] courseVideos = req.getParameterValues("courseVideo");

        //调用service层删除方法
        if(courseService.delAll(cids)){
            for (String courseImage : courseImages) {
                //获取图片地址
                File file = new File("E:\\Users\\tan\\Desktop\\upload",courseImage);
                //删除图片
                file.delete();
            }
            for (String courseVideo : courseVideos) {
                //获取视频地址
                File file = new File("E:\\Users\\tan\\Desktop\\upload",courseVideo);
                //删除视频
                file.delete();
            }
            resultVO = new ResultVO(200,"删除成功",null);
        }else {
            resultVO = new ResultVO(500,"删除失败",null);
        }
        //响应数据
        String json = objectMapper.writeValueAsString(resultVO);
        resp.getWriter().println(json);
    }

    /**
     * 上传文件
     */
    public void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //调用fileUpload工具类获取保存文件 返回文件名
            String fileName = FileUploadUtil.fileUpload(req);
            //如果包含以下格式表示图片 否则是视频
            if(fileName.contains(".jpg")||fileName.contains(".png")||fileName.contains(".bmp")||fileName.contains(".gif")){
                resultVO = new ResultVO(201,"上传图片成功",fileName);
            }else {
                resultVO = new ResultVO(202,"上传视频成功",fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //出现异常回传失败
            resultVO = new ResultVO(500,"上传视频失败",null);
            throw new RuntimeException();
        }  finally {
            //转换为json格式
            String json = objectMapper.writeValueAsString(resultVO);
            //输出响应
            resp.getWriter().println(json);
        }

    }

    /**
     * 删除未确定的上传文件
     */
    public void deleteFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取文件名参数
        String fileName = req.getParameter("fileName");
        //获取文件地址
        File file = new File("E:\\Users\\tan\\Desktop\\upload",fileName);
        //删除文件
        file.delete();
    }

    /**
     *  添加课程
     */
    public void addCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        //使用beanUtils工具类获取参数 封装对象
        BeanUtils.populate(course,req.getParameterMap());
        //调用service层方法添加
        if(courseService.addCourse(course)){
            //添加成功
            resultVO = new ResultVO(200,"添加成功",null);
        }else {
            //添加失败
            resultVO = new ResultVO(500,"添加失败",null);
        }
        //响应数据
        String json = objectMapper.writeValueAsString(resultVO);
        resp.getWriter().println(json);
    }

    /**
     * 修改课程
     */
    public void amendCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        //使用beanUtils工具类获取参数 封装对象
        BeanUtils.populate(course,req.getParameterMap());
        String courseImage = req.getParameter("fileImg");
        String courseVideo = req.getParameter("fileVideo");
        //调用service层方法修改
        if(courseService.amendCourse(course,courseImage,courseVideo)){
            //修改成功
            resultVO = new ResultVO(200,"修改成功",null);
        }else {
            //修改失败
            resultVO = new ResultVO(500,"修改失败",null);
        }
        //响应数据
        String json = objectMapper.writeValueAsString(resultVO);
        resp.getWriter().println(json);
    }

    /**
     * 查询课程
     */
    public void findAllCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Course> course = courseService.findAllCourse();
        resp.getWriter().println(objectMapper.writeValueAsString(course));
    }

}
